package io.renren.modules.seller.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.seller.entity.OrderEntity;
import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.Result;
import io.renren.modules.seller.service.OrderService;
import io.renren.modules.sys.shiro.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mark  15373178086@163.com
 * @date 2020-02-16 11:30:55
 */
@Slf4j
@RestController
@RequestMapping("seller/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 返回全部列表
     *
     * @return TbBrands
     */
    @RequestMapping("/findAll")
    public List<OrderEntity> findAll() {
        Long userId = ShiroUtils.getUserId();
        Wrapper<OrderEntity> wrapper = new QueryWrapper<OrderEntity>().eq("seller_id", userId);
        return orderService.list(wrapper);
    }

    /**
     * 查询+分页
     *
     * @return PageResult
     */
    @RequestMapping("/search")
    public PageResult search(@RequestBody OrderEntity orderEntity, String page, String limit) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("page", page);
        params.put("limit", limit);
        log.info("order search params:{}", params);
        return orderService.findPage(orderEntity, params);
    }

    /**
     * 返回全部列表
     *
     * @return PageResult
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestParam Map<String, Object> params) {
        log.info("brand findPage params:{}", params);
        return orderService.findPage(params);
    }

    /**
     * 修改-发货
     * ['0', '1未付款', '2交易关闭', '3已付款', '4已发货', '5交易成功', '6待评价', '7已评价'];
     *
     * @return Result
     */
    @RequestMapping("/update")
    public Result update(@RequestBody OrderEntity orderEntity) {
        try {
            OrderEntity byId = orderService.getById(orderEntity.getOrderId());
            if (!byId.getSellerId().equals(ShiroUtils.getUserId().toString())) {
                return Result.fail("订单与商家不匹配");
            }
            OrderEntity order = new OrderEntity();
            order.setOrderId(byId.getOrderId());
            order.setStatus("4");
            order.setPostFee(orderEntity.getPostFee());
            order.setShippingCode(orderEntity.getShippingCode());
            order.setShippingName(orderEntity.getShippingName());
            order.setUpdateTime(new Date());
            order.setConsignTime(order.getUpdateTime());
            orderService.updateById(order);
            return Result.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail();
        }
    }

    /**
     * 获取实体
     *
     * @param id id
     * @return OrderEntity
     */
    @RequestMapping("/findOne")
    public OrderEntity findOne(Long id) {
        OrderEntity byId = orderService.getById(id);
        if (byId != null && ShiroUtils.getUserId().toString().equals(byId.getSellerId())) {
            return byId;
        }
        return new OrderEntity();
    }

    /**
     * 1、未付款，2、已付款，3、未发货，4、已发货，5、交易成功，6、交易关闭,7、待评价
     *
     * @return Result
     */
    @RequestMapping("/updateStatus")
    public Result updateStatus(Long id, String status) {
        try {
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.setOrderId(id);
            orderEntity.setStatus(status);
            orderEntity.setUpdateTime(new Date());
            orderService.updateById(orderEntity);
            return Result.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail();
        }
    }
}
